package com.unitd.base.frame.utils;

import org.apache.commons.beanutils.BeanPredicate;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.PredicateUtils;
import org.apache.commons.collections.functors.EqualPredicate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * List集合工具类, 此类中定义一些对于集合操作的共有方法, 方便后续工程代码调用
 * @filename ListUtils.java
 * @copyright www.unitd.com
 * @author Hudan
 * @version 1.0
 * @date 2017/4/19
 */
public class ListUtils extends org.apache.commons.collections.ListUtils {

	/**
	 * 判断指定的List对象是否为空
	 * @param list list集合对象
	 * @return boolean
	 */
	public static boolean isNull(List list) {
		if (list != null && !list.isEmpty() && list.size() > 0) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 将字符串List集合按照指定的分割符号转换成字符串
	 * @param list  需要转换的字符串List集合对象
	 * @param split 将字符串List集合元素解析组成字符串时的每个分隔符
	 * @return String
	 */
	public static String toStr(List<String> list, String split) {
		if (isNull(list)) {
			return "";
		}

		String retStr;
		StringBuilder sb = new StringBuilder();
		for (String str : list) {
			sb.append(str).append(split);
		}
		retStr = sb.toString();
		return retStr.substring(0, retStr.length() - 1);
	}

	/**
	 * @param list        需要筛选的List集合对象
	 * @param columnName  需要筛选的属性名称(List里面的对象的属性字段:例如  name)
	 * @param columnValue 需要筛选的属性对应的值(name字段的属性值,也就是你要筛选出 name的名称为 “张三” 的所有list)
	 * @return
	 * 按照指定的属性名和该属性对应的值, 对List集合进行筛选
	 */
	public static Collection<?> checkList(List<?> list, String columnName, Object columnValue) {
		EqualPredicate parameter = new EqualPredicate(columnValue);
		BeanPredicate tableCoulmn = new BeanPredicate(columnName, parameter);
		Predicate[] allPredicateArray = {tableCoulmn};
		Predicate allPredicate = PredicateUtils.allPredicate(allPredicateArray);
		Collection<?> filteredCollection = CollectionUtils.select(list, allPredicate);
		return filteredCollection;
	}

	/**
	 * @param <T>   需要拆分的List对象类型
	 * @param list  要拆分的集合
	 * @param count 每个集合的元素个数
	 * @return 返回拆分后的各个集合
	 * 按照指定大小拆分集合
	 */
	public static <T> List<List<T>> splitList(List<T> list, int count) {

		if (isNull(list)) {
			return null;
		}

		List<List<T>> ret = new ArrayList<>();
		int size = list.size();
		// 数据量不足count指定的大小
		if (size <= count) {
			ret.add(list);
		} else {
			int pre = size / count;
			int last = size % count;
			// 前面pre个集合,每个大小都是count个元素
			for (int i = 0; i < pre; i++) {
				List<T> itemList = new ArrayList<T>();
				for (int j = 0; j < count; j++) {
					itemList.add(list.get(i * count + j));
				}
				ret.add(itemList);
			}
			// last的进行处理
			if (last > 0) {
				List<T> itemList = new ArrayList<T>();
				for (int i = 0; i < last; i++) {
					itemList.add(list.get(pre * count + i));
				}
				ret.add(itemList);
			}
		}
		return ret;
	}

	public static void main(String[] args) {
		List<String> resList = Arrays.asList("0", "1", "2", "3", "4", "5", "6",
				"7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
				"18", "19", "20", "21", "22", "23", "24", "25", "26", "27",
				"28", "29", "30", "31", "32", "33", "34", "35", "36", "37",
				"38", "39", "40", "41", "42", "43", "44", "45", "46", "47",
				"48", "49", "50", "51", "52", "53", "54", "55", "56", "57",
				"58", "59", "60", "61", "62", "63", "64", "65", "66", "67",
				"68", "69", "70", "71", "72", "73", "74", "75", "76", "77",
				"78", "79", "80", "81", "82", "83", "84", "85", "86", "87",
				"88", "89", "90", "91", "92", "93", "94", "95", "96", "97",
				"98", "99");
		List<List<String>> ret = splitList(resList, 15);

		for (int i = 0; i < ret.size(); i++) {
			System.out.println(ret.get(i));
		}
	}
}